package com.android.fastride.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.fastride.Constant;
import com.android.fastride.R;
import com.android.fastride.activity.base.BaseActivity;
import com.android.fastride.model.db.entity.FinishedOrdersCountResponse;
import com.android.fastride.model.repository.ControlSystemRepository;
import com.android.fastride.view.fragment.CompletedOrderListFragment;
import com.android.fastride.view.widget.TopBar;
import com.peng.android.coremodule.utils.RxSchedulerUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;

public class CompletedOrderListActivity extends BaseActivity{
    /*private static final String REQUEST_DATE = "request_date";
    private static final int REQUEST_CODE_START_DATE = 1;
    private static final int REQUEST_CODE_END_DATE = 2;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM.dd.yyyy");*/
    private FragmentManager fragmentManager;

    Unbinder unbinder;
    @BindView(R.id.topbar)
    TopBar topbar;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.tv_start_date)
    TextView tvStartDate;
    @BindView(R.id.iv_pick_start_date)
    ImageView ivPickStartDate;
    @BindView(R.id.tv_end_date)
    TextView tvEndDate;
    @BindView(R.id.iv_pick_end_date)
    ImageView ivPickEndDate;
    @BindView(R.id.ib_search)
    ImageView ibSearch;
    @BindView(R.id.tab_viewpager)
    ViewPager tabViewpager;


//    private Fragment[] mFragmentArrays = new Fragment[2];
    private String[] mTabTitles = new String[]{"已完成","已取消"};
    private List<Fragment> mFragmentList;
    FragmentPagerAdapter mFragmentPagerAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, CompletedOrderListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_completed_order);
        unbinder = ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        initView();
        initData();
    }

    /**
     * 增加
     */
    private void initData() {
        ControlSystemRepository.getFinishedCount()
                .compose(RxSchedulerUtils.normalSchedulersTransformer())
                .subscribe(new Subscriber<FinishedOrdersCountResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        // 错误信息
                    }

                    @Override
                    public void onNext(FinishedOrdersCountResponse finishedOrdersCountResponse) {
                        // 获取到数据，刷新列表
                        mTabTitles[0] = mTabTitles[0] +"("+ finishedOrdersCountResponse.getFinished() +")";
                        mTabTitles[1] = mTabTitles[1] +"("+ finishedOrdersCountResponse
                                .getCanceled() +")";
                        mFragmentPagerAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void initView() {
        mFragmentList = new ArrayList();
        mFragmentList.add(CompletedOrderListFragment.newInstance(Constant.ORDER_COMPLETED));
        mFragmentList.add(CompletedOrderListFragment.newInstance(Constant.ORDER_CANCEL));

        topbar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {
            @Override
            public void OnLeftButtonClick() {
                CompletedOrderListActivity.this.finish();
            }

            @Override
            public void OnRightButtonClick() {

            }
        });

        mFragmentPagerAdapter = new FragmentPagerAdapter
                (getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position%2);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTabTitles[position%2];
            }
        };

        tabViewpager.setAdapter(mFragmentPagerAdapter);
        tablayout.setupWithViewPager(tabViewpager);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /*@OnClick({R.id.tv_start_date, R.id.iv_pick_start_date, R.id.tv_end_date, R.id
            .iv_pick_end_date, R.id.ib_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_start_date:
            case R.id.iv_pick_start_date:
                pickerDate(REQUEST_CODE_START_DATE, stringToDate(tvStartDate.getText().toString()));
                break;
            case R.id.tv_end_date:
            case R.id.iv_pick_end_date:
                pickerDate(REQUEST_CODE_END_DATE, stringToDate(tvEndDate.getText().toString()));
                break;
            case R.id.ib_search:
                break;
        }

    }

    private void pickerDate(int requestCode, Date date) {
        DatePickerFragment datePickerFragment = DatePickerFragment.newInstance(date);
        datePickerFragment.setSelectedDateListener(this, requestCode);
        datePickerFragment.show(fragmentManager, REQUEST_DATE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
        if (resultCode != Activity.RESULT_OK || date == null) {
            return;
        }

        if (requestCode == REQUEST_CODE_START_DATE) {
            tvStartDate.setText(dateToString(date));
        } else if (requestCode == REQUEST_CODE_END_DATE) {
            tvEndDate.setText(dateToString(date));
        }

    }

    public String dateToString(Date date) {
        return simpleDateFormat.format(date);
    }

    public Date stringToDate(String dateStr) {
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    private void setTextStyle(TextView view, int val) {
        setTextStyle(view, String.valueOf(val));
    }

    private void setTextStyle(TextView view, float val) {
        setTextStyle(view, String.valueOf(val));
    }

    private void setTextStyle(TextView view, String val) {
        String values = null;
        if (val.contains(".")) {
            values = val;
        } else {
            values = val + ".00";
        }
        int position = values.indexOf(".");
        SpannableString styleText = new SpannableString(values);
        styleText.setSpan(new TextAppearanceSpan(getApplicationContext(), R.style.bigGreenTextStyle), 0,
                position + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        styleText.setSpan(new TextAppearanceSpan(this, R.style.smallGreenTextStyle),
                position + 1, values.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(styleText, TextView.BufferType.SPANNABLE);
    }

    @Override
    public void onSelectedDate(int requestCode, Date date) {
        if (requestCode == REQUEST_CODE_START_DATE) {
            tvStartDate.setText(dateToString(date));
        } else if (requestCode == REQUEST_CODE_END_DATE) {
            tvEndDate.setText(dateToString(date));
        }
    }*/
}
